package com.zfei.gfb.controller;

import com.google.common.collect.Lists;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbHouseApplyMapper;
import com.zfei.gfb.dao.GfbHouseMapper;
import com.zfei.gfb.model.GfbHouse;
import com.zfei.gfb.model.GfbHouseApply;
import com.zfei.gfb.query.GfbHouseApplyQuery;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.GfbCompanyVo;
import com.zfei.gfb.vo.GfbHouseApplyListVo;
import com.zfei.gfb.vo.GfbHouseApplyVo;
import com.zfei.gfb.vo.GfbHouseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
/**
 * @version 1.0
 * @author
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "房屋预约接口")
@RestController
@RequestMapping("/web/house/apply")
public class GfbHouseApplyController  {

	@Resource
	private GfbHouseApplyMapper gfbHouseApplyMapper;

	@Resource
	private GfbHouseMapper gfbHouseMapper;

	@ApiOperation(value = "预约列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbHouseApplyListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list(@RequestBody GfbHouseApplyQuery query) {
		query.build(Now::millis);

		GfbHouseApplyListVo result = new GfbHouseApplyListVo();

		int total = gfbHouseApplyMapper.totalWeb(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbHouseApply> records = gfbHouseApplyMapper.listWeb(query);
		List<GfbHouseApplyVo> vos  = Beans.copys(records,GfbHouseApplyVo.class);
		result.setData(vos);
		return result;

	}

	@ApiOperation(value = "详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "详情", response = GfbHouseApplyVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbHouseApply houseApply = gfbHouseApplyMapper.selectByPrimaryKey(id);
		GfbHouseApplyVo vo = Beans.copy(houseApply, GfbHouseApplyVo.class);

		GfbHouse house = gfbHouseMapper.selectByPrimaryKey(houseApply.getHouseId());
		GfbHouseVo houseVo = Beans.copy(house, GfbHouseVo.class);
		//房屋图片
		List<String> images = Gsons.fromJson(house.getImages(), Types.StringListType);
		houseVo.setImages(images);
		//消防图片
		List<String> checkImages = Gsons.fromJson(house.getCheckImages(), Types.StringListType);
		houseVo.setCheckImages(checkImages);

		vo.setGfbHouseVo(houseVo);
		return vo;
	}
}
